clear all
set more off, perm
local mydir "`1'"
global Tables	`mydir'/XS/Tables
global Work	`mydir'/XS/DataWork
global Raw	`mydir'/XS/DataRaw
global WRDS	`mydir'/WRDS
/* ********************************************************************* */
* ADD RETURNS
import sas $WRDS/CRSP/msf.sas7bdat, clear case(lower)
keep permno permco date prc 
replace prc=abs(prc)

tempfile prc
save "`prc'", replace
* *********************
import sas using $WRDS/CRSP/stocknames.sas7bdat, clear case(lower)
rename *, lower

keep if (shrcd==10 | shrcd==11) & (exchcd>=1 & exchcd<=3)

keep permco permno namedt nameenddt 
duplicates drop

* find EOM date for last record of each stock
sort permco permno nameenddt
bys permco permno:  gen obs=_n
bys permco permno:  gen tot=_N
replace nameenddt=mdy(12,31,year(nameenddt)) 			if month(nameenddt)==12 & obs==tot
replace nameenddt=mdy(month(nameenddt)+1,1,year(nameenddt))-1	if month(nameenddt)<=11 & obs==tot
drop obs tot

joinby permco permno using  $Work/msf_adjusted
keep if date>=namedt & date<=nameenddt 
keep if mcap<.

* a few observations (1.0%) have overlapping namedt-nameenddt ranges 
drop namedt nameenddt
duplicates drop

format date %td
* *********************
* add stock price
merge 1:1 permco permno date using "`prc'"
keep if _merge==3
drop _merge
* [1] Get rid of multiple observations per firm/date
* [1.a] Don't bother weighting observations if firm has only one class of shares
sort permco permno date mcap
bys permco date: gen obs=_n 
bys permco date: gen tot=_N 
sort permco date mcap
order tot obs permco permno date mcap

generate vwret = ret  if tot==1
generate vwretx= retx if tot==1

* [1.b] weigh returns by lagged market cap when there is more than one security
egen id=group(permco permno)
tsset id datem

generate Lmcap=L1.mcap
replace  Lmcap=mcap/(1+retx)	if Lmcap==.
bys permco date: egen LMCAP=total(Lmcap)

replace vwret =(Lmcap/LMCAP)*ret	if tot>1
replace vwretx=(Lmcap/LMCAP)*retx	if tot>1

* Compute D/P
tsset id datem
gen Ra =0
gen Rxa=0
gen  D =0
forvalues x=0(1)11 {
	replace  Ra  = (1+Ra )*(1+L`x'.ret )
	replace  Rxa = (1+Rxa)*(1+L`x'.retx)
	replace    D = D+(L`x'.mcap)*((1+L`x'.ret )/(1+L`x'.retx)-1)
}
bys permco date: egen  MCAP=total(mcap )
generate DP = Ra/Rxa-1 /* (P1+D1)/P0 * P0/P1 -1 */   
replace  DP = (mcap/MCAP)*DP

* [1.b] Compute vw return; sort data to keep permno for the security with the largest market cap @t
sort permco datem mcap
collapse (lastnm) permno exchcd comnam date (sum) Mcap=mcap ret=vwret retx=vwretx DP D Lmcap (count) nr=ret nx=retx nDP=DP, by(permco datem)
drop if Mcap==0
replace  ret =. if    nr==0
replace  retx=. if    nx==0
replace    DP=. if   nDP==0
replace     D=. if   nDP==0
replace Lmcap=. if Lmcap==0

format datem %tm 
format date %td 

tsset permco datem
generate mcap0=Mcap
rename Mcap Mcap0
forvalues x=1(1)60 {
	local j=`x'-1
	quietly: generate   ret`x'=F`x'.ret 
	quietly: generate  retx`x'=F`x'.retx 
	quietly: generate mcap`x'=mcap`j'*(1+F`x'.ret ) 
	quietly: generate Mcap`x'=Mcap`j'*(1+F`x'.retx) 
}
rename ret  ret0
rename retx retx0
order *, sequential
order comnam date datem permco permno ret0-ret60 retx0-retx60 Mcap0-Mcap60 mcap0-mcap60 Lmcap DP D
keep  comnam date datem permco permno ret0-ret60 retx0-retx60 Mcap0-Mcap60 mcap0-mcap60 Lmcap DP D
* *******************************
save $Work/FamaFrench02Ret, replace

*"datem" in /srv/update/FamaFrench01 is the formation date (i.e. June of year t)
keep if month(date)==6
merge 1:1 permco datem using $Work/FamaFrench01
keep if _merge==3
drop _merge
order permco date datem datadate 
save $Work/FamaFrench02, replace
* *******************************
